package com.tools.demo.sign;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpUtil;
import com.tools.demo.pbTools.Base64Tools;
import com.tools.demo.sign.utils.HttpsDownload;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.xpath.XPathConstants;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 测试盖章
 *
 * @author mfk
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
public class SignController
{

    @PostMapping("/sign")
    public Object sign(int page, double x, double y) throws Exception
    {
        double xin = x/730 * 50000;
        double yin = y/880 * 50000;

        String path = "/Users/admin/Documents/3.Code/Tools_Demo/static/test.pdf";
        String base64 = Base64Tools.fileToBase64(path);

        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><SEAL_DOC_REQUEST> \n" +
                "<BASE_DATA> \n" +
                "<SYS_ID>sysId</SYS_ID> \n" +
                "\t\t<SYS_PWD>sysId</SYS_PWD> \n" +
                "</BASE_DATA> \n" +
                "<META_DATA> \n" +
                "<IS_MERGER>false</IS_MERGER> \n" +
                "<IS_CODEBAR>false</IS_CODEBAR> \n" +
                "</META_DATA> \n" +
                "<FILE_LIST> \n" +
                "<TREE_NODE> \n" +
                "<FILE_NO>3421.pdf</FILE_NO> \n" +
                "<CJ_TYPE>BASE64</CJ_TYPE> \n" +
                "<MODEL_NAME>"+base64+"</MODEL_NAME>\n" +
                "<REQUEST_TYPE>0</REQUEST_TYPE> \n" +
                "<IS_MERGER>false</IS_MERGER> \n" +
                "<IS_CODEBAR>false</IS_CODEBAR> \n" +
                "<DOC_TYPE>pdf</DOC_TYPE> \n" +
                "<RULE_TYPE>1</RULE_TYPE> \n" +
                "<RULE_NO>gtxd</RULE_NO> \n" +
                "<RULE_LIST> \n" +
                "<RULE_NODE> \n" +
                "<RULE_INFO> "+ (page - 1) +" ,"+xin+",5,5,"+yin+",</RULE_INFO> \n" +
                "<SEAL_NAME>电子政务测试公章1</SEAL_NAME> \n" +
                "</RULE_NODE> \n" +
                "</RULE_LIST> \n" +
                "</TREE_NODE> \n" +
                "</FILE_LIST> \n" +
                "</SEAL_DOC_REQUEST>";
        System.out.println(page +"," + x + "," + y);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("xmlStr", xml);
        String res = HttpUtil.createPost("https://172.18.108.39:8082/ESS/sealAutoPdfZF")
                .form(map)
                .execute()
                .body();
        org.w3c.dom.Document docResult = XmlUtil.readXML(res);

        String url = XmlUtil.getByXPath("//SEAL_DOC_RESPONSE/FILE_LIST/FILE/FILE_URL", docResult, XPathConstants.STRING).toString();
        byte[] pdfByte = HttpsDownload.download(url);
        String pdfData = Base64.getEncoder().encodeToString(pdfByte);
        String fileName = UUID.randomUUID() +  ".pdf";
        String targetUrl = "/Users/admin/Documents/3.Code/Tools_Demo/static/" + fileName;
        Base64Tools.decodeBase64File(pdfData, targetUrl);

        return fileName;

    }


    @GetMapping("getFile")
    public void getFile(HttpServletRequest request, HttpServletResponse response){
        try {
            File file = new File("/Users/admin/Documents/3.Code/Tools_Demo/static/"+request.getParameter("fileName"));
            FileInputStream fileInputStream = new FileInputStream(file);
            response.setHeader("Content-Type", "application/pdf");
            OutputStream outputStream = response.getOutputStream();
            IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }


}
